//
//  YPZQRecodeCell.m
//  ChengHuiTong
//
//  Created by CHT-Technology on 2017/2/22.
//  Copyright © 2017年 张文权. All rights reserved.
//

#import "YPZQRecodeCell.h"

@interface YPZQRecodeCell (){
    
    //公共
    UIView *_contentView;
    UILabel *_titleLabel;
    
    //可转让,已认购
    UILabel *_leftTimeLabel;
    
    //转让中，已转让
    UILabel *_zrTimeLabel;
    
    //可转让
    UILabel *_projectTimeLabel; //项目到期时间
    UIButton *_applyZrBtn; //申请转让
    
    //已认购
    UIButton *_hkDetailBtn; //还款明细
    
}

@end

@implementation YPZQRecodeCell

#define contentW (SCREEN_WIDTH - leftMargin*2)

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        CGFloat sepeH = 10;
        self.selectionStyle = 0;
        self.contentView.backgroundColor = UIColorFromRGB(0xf5f5f5);;
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, sepeH, SCREEN_WIDTH, 0)];
        _contentView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_contentView];
        
        
        _titleLabel = [UILabel creatWithFrame:CGRectMake(leftMargin, 0, 15*17, 40)
                                         font:[UIFont systemFontOfSize:15]
                                         text:@"幕墙玻璃工程公司资金周转（到期还本第3标）"
                                    textColor:[UIColor blackColor]
                                textAlignment:0];
        [_contentView addLineWithFrame:CGRectMake(0, _titleLabel.bottom, SCREEN_WIDTH, 0.5) color:sepeLineColor];
        [_contentView addSubview:_titleLabel];
        
        ;\
        CGFloat contentBottom = [self getMiddleBottomAndAddLabelWithReuseIdentifier:reuseIdentifier];
        [_contentView addLineWithFrame:CGRectMake(leftMargin, contentBottom-0.5, SCREEN_WIDTH - leftMargin*2, 0.5) color:sepeLineColor];
        
        if ([reuseIdentifier isEqualToString:@"转让中"] || [reuseIdentifier isEqualToString:@"已转让"]) {
            _zrTimeLabel = [UILabel creatWithFrame:CGRectMake(leftMargin, contentBottom, contentW, 30)
                                              font:[UIFont systemFontOfSize:12]
                                              text:@"转让到期时间："
                                         textColor:kColor_greyAshTitleColor
                                     textAlignment:0];
            [_contentView addSubview:_zrTimeLabel];
            contentBottom = _zrTimeLabel.bottom;
            
        }else{
            _leftTimeLabel = [UILabel creatWithFrame:CGRectMake(leftMargin, contentBottom, contentW/2.f, 30)
                                                font:[UIFont systemFontOfSize:12]
                                                text:@"下期还款："
                                           textColor:kColor_greyAshTitleColor
                                       textAlignment:0];
            [_contentView addSubview:_leftTimeLabel];
            
            
            
            if ([reuseIdentifier isEqualToString:@"可转让"]) {
                _projectTimeLabel = [UILabel creatWithFrame:CGRectMake(_leftTimeLabel.right, _leftTimeLabel.top, _leftTimeLabel.width, _leftTimeLabel.height)
                                                       font:_leftTimeLabel.font
                                                       text:@"项目到期："
                                                  textColor:_leftTimeLabel.textColor
                                              textAlignment:0];
                [_contentView addSubview:_projectTimeLabel];
                
                
                _applyZrBtn = [UIButton creatWithFrame:CGRectMake(leftMargin, _leftTimeLabel.bottom + sepeH*2, contentW, 40)
                                                  font:[UIFont systemFontOfSize:16]
                                                  text:@"申请转让"
                                                action:@selector(applyAction)
                                                target:self];
                _applyZrBtn.backgroundColor = kColor_AppNavColor;
                _applyZrBtn.layer.cornerRadius = _applyZrBtn.height/2.f;
                _applyZrBtn.layer.masksToBounds = YES;
                [self.contentView addSubview:_applyZrBtn];
                
            }else if ([reuseIdentifier isEqualToString:@"已认购"]){
                _hkDetailBtn = [UIButton creatWithFrame:CGRectMake(_leftTimeLabel.right + (_leftTimeLabel.width-90)/2.f, _leftTimeLabel.top+5, 90, 20)
                                                  font:[UIFont systemFontOfSize:12]
                                                  text:@"还款明细"
                                              textColor:kColor_AppNavColor
                                                action:@selector(hkDetailAction)
                                                target:self];
                _hkDetailBtn.layer.cornerRadius = _hkDetailBtn.height/2.f;
                [_hkDetailBtn setBorderWithColor:kColor_AppNavColor borderWidth:1];
                [_contentView addSubview:_hkDetailBtn];
                
            }
            contentBottom = _leftTimeLabel.bottom;
        }
        
        _contentView.height = contentBottom;
        [_contentView addLine];
    }
    
    return self;
}

- (CGFloat)getMiddleBottomAndAddLabelWithReuseIdentifier:(NSString *)reuseIdentifier{
    
    CGFloat bottom = _titleLabel.bottom;
    NSInteger count = [reuseIdentifier isEqualToString:@"已认购"]?6:4;
    for (int i = 0; i < count; i ++) {
        
        UILabel *label = [UILabel creatWithFrame:CGRectMake(leftMargin + (i%2)*contentW/2.f, bottom + 10 + (i/2)*20, contentW/2.f, 20)
                                         font:[UIFont systemFontOfSize:12]
                                         text:@"债券价值"
                                    textColor:kColor_greyAshTitleColor
                                textAlignment:0];
        label.tag = 1000+i;
        [_contentView addSubview:label];
        if(i == count-1) bottom = label.bottom + 10;
    }
    
    return bottom;
}

- (NSAttributedString *)getAttributedStringWithChangeTextColor:(UIColor *)textColor
                                                originalString:(NSString *)originalString
                                                  changeString:(NSString *)changeString
{
    
    NSMutableAttributedString *mAttString = [[NSMutableAttributedString alloc]initWithString:originalString];
    [mAttString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:SCREEN_WIDTH>320?14:12],
                                NSForegroundColorAttributeName:textColor,
                                } range:[originalString rangeOfString:changeString]];
    if ([originalString rangeOfString:@"元"].location != NSNotFound) {
        [mAttString addAttribute:NSForegroundColorAttributeName value:textColor range:[originalString rangeOfString:@"元"]];
        
    }
    if ([originalString rangeOfString:@"%"].location != NSNotFound) {
        [mAttString addAttribute:NSForegroundColorAttributeName value:textColor range:[originalString rangeOfString:@"%"]];
       
    }
    return mAttString;
}

#pragma mark -- 设置模型
//可转让
- (void)setYPZQRecordModel:(YPZQRecordModel *)yPZQRecordModel{
    
    _yPZQRecordModel = yPZQRecordModel;
    
    _titleLabel.text = _yPZQRecordModel.title;
    
    _applyZrBtn.backgroundColor = _yPZQRecordModel.transferable.integerValue == 0?kColor_AppNavColor:[UIColor lightGrayColor];
    
//    _applyZrBtn.enabled = _yPZQRecordModel.transferable.integerValue == 0?YES:NO;
    
    _leftTimeLabel.attributedText = [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                  originalString:[NSString stringWithFormat:@"下期还款 :  %@",_yPZQRecordModel.next_repay_date]
                                                                    changeString:_yPZQRecordModel.next_repay_date];
    
    _projectTimeLabel.attributedText = [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                     originalString:[NSString stringWithFormat:@"项目到期 :  %@",_yPZQRecordModel.last_repay_date]
                                                                       changeString:_yPZQRecordModel.last_repay_date];
    
    
    [self refreshMiddleViewsWithTextArray:@[[self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"投资金额 :  %@ 元",_yPZQRecordModel.tender_money]
                                                                            changeString:_yPZQRecordModel.tender_money],
                                            
                                            [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"剩余本金 :  %@ 元",_yPZQRecordModel.wait_capital]
                                                                            changeString:_yPZQRecordModel.wait_capital],
                                            
                                            [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"原标利率 :  %@ %%",_yPZQRecordModel.apr]
                                                                            changeString:_yPZQRecordModel.apr],
                                            
                                            [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"剩余期限 :  %@/%@",_yPZQRecordModel.remain_periods,_yPZQRecordModel.total_periods]
                                                                            changeString:[NSString stringWithFormat:@"%@/%@",_yPZQRecordModel.remain_periods,_yPZQRecordModel.total_periods]]]];

}

//转让中
- (void)setYPZQZRZModel:(YPZQZRZModel *)yPZQZRZModel{
    
    _yPZQZRZModel = yPZQZRZModel;
    
    _titleLabel.text = _yPZQZRZModel.title;
    
    _zrTimeLabel.attributedText = [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                     originalString:[NSString stringWithFormat:@"转让到期时间 :  %@",_yPZQZRZModel.expire_time]
                                                                       changeString:_yPZQZRZModel.expire_time];
    
    
    [self refreshMiddleViewsWithTextArray:@[[self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"债权价值 :  %@ 元",_yPZQZRZModel.credit_value]
                                                                            changeString:_yPZQZRZModel.credit_value],
                                            
                                            [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"待还本金 :  %@ 元",_yPZQZRZModel.wait_capital]
                                                                            changeString:_yPZQZRZModel.wait_capital],
                                            
                                            [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"原标利率 :  %@ %%",_yPZQZRZModel.apr]
                                                                            changeString:_yPZQZRZModel.apr],
                                            
                                            [self getAttributedStringWithChangeTextColor:kColor_redNumberColor
                                                                          originalString:[NSString stringWithFormat:@"转让金额 :  %@ 元",_yPZQZRZModel.sale_price]
                                                                            changeString:_yPZQZRZModel.sale_price]]];
}

//已转让
- (void)setYPZQYZRModel:(YPZQYZRModel *)yPZQYZRModel{
    
    _yPZQYZRModel = yPZQYZRModel;
    
    _titleLabel.text = _yPZQYZRModel.title;
    
    _zrTimeLabel.attributedText = [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                originalString:[NSString stringWithFormat:@"转让时间 :  %@",_yPZQYZRModel.buy_time]
                                                                  changeString:_yPZQYZRModel.buy_time];
    
    
    [self refreshMiddleViewsWithTextArray:@[[self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"债权价值 :  %@ 元",_yPZQYZRModel.credit_value]
                                                                            changeString:_yPZQYZRModel.credit_value],
                                            
                                            [self getAttributedStringWithChangeTextColor:kColor_redNumberColor
                                                                          originalString:[NSString stringWithFormat:@"回收本金 :  %@ 元",_yPZQYZRModel.return_capital]
                                                                            changeString:_yPZQYZRModel.return_capital],
                                            
                                            [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"扣除奖励 :  %@ 元",_yPZQYZRModel.award]
                                                                            changeString:_yPZQYZRModel.award],
                                            
                                            [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"扣除手续费 :  %@ 元",_yPZQYZRModel.sale_fee]
                                                                            changeString:_yPZQYZRModel.sale_fee]]];
}

//已认购
- (void)setYPZQYRGModel:(YPZQYRGModel *)yPZQYRGModel{
    
    _yPZQYRGModel = yPZQYRGModel;
    
    _titleLabel.text = _yPZQYRGModel.title;
    
    _leftTimeLabel.attributedText = [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                  originalString:[NSString stringWithFormat:@"认购时间 :  %@",_yPZQYRGModel.buy_time]
                                                                    changeString:_yPZQYRGModel.buy_time];

    [self refreshMiddleViewsWithTextArray:@[[self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"债权价值 :  %@ 元",_yPZQYRGModel.credit_value]
                                                                            changeString:_yPZQYRGModel.credit_value],
                                            
                                            [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"认购金额 :  %@ 元",_yPZQYRGModel.sale_price]
                                                                            changeString:_yPZQYRGModel.sale_price],
                                            
                                            [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"待收本金 :  %@ 元",_yPZQYRGModel.wait_capital]
                                                                            changeString:_yPZQYRGModel.wait_capital],
                                            
                                            [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"剩余期限 :  %@/%@",_yPZQYRGModel.remain_periods,_yPZQYRGModel.total_periods]
                                                                            changeString:[NSString stringWithFormat:@"%@/%@",_yPZQYRGModel.remain_periods,_yPZQYRGModel.total_periods]],
                                            
                                            [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"原标利率 :  %@ %%",_yPZQYRGModel.apr]
                                                                            changeString:_yPZQYRGModel.apr],
                                            [self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                                          originalString:[NSString stringWithFormat:@"项目状态 :  %@",_yPZQYRGModel.repay_status.integerValue == 0?@"还款中":@"已结束"]
                                                                            changeString:_yPZQYRGModel.repay_status.integerValue == 0?@"还款中":@"已结束"]]
    
     ];
}


- (void)refreshMiddleViewsWithTextArray:(NSArray *)textArray{
    
    [textArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *label = [_contentView viewWithTag:idx+1000];
        label.attributedText = obj;
    }];
}

#pragma mark -- 事件响应
- (void)applyAction{
    
    if (self.applyBlock) {
        self.applyBlock(_yPZQRecordModel);
    }
}
- (void)hkDetailAction{
    
    if (self.hkDetailBlock) {
        self.hkDetailBlock(_yPZQYRGModel.repay_list);
    }
}

@end
